load("//bazel:ray.bzl", "flatbuffer_java_library", "define_java_module")

exports_files([
    "testng.xml",
    "checkstyle.xml",
    "checkstyle-suppressions.xml",
    "streaming/testng.xml",
])

java_binary(
    name = "bazel_deps",
    main_class = "com.github.johnynek.bazel_deps.ParseProject",
    runtime_deps = ["@bazel_deps//jar"],
)

java_import(
    name = "all_modules",
    jars = [
        "liborg_ray_ray_api.jar",
        "liborg_ray_ray_api-src.jar",
        "liborg_ray_ray_runtime.jar",
        "liborg_ray_ray_runtime-src.jar",
        "liborg_ray_ray_tutorial.jar",
        "liborg_ray_ray_tutorial-src.jar",
        "liborg_ray_ray_streaming.jar",
        "liborg_ray_ray_streaming-src.jar",
        "all_tests_deploy.jar",
        "all_tests_deploy-src.jar",
        "streaming_tests_deploy.jar",
        "streaming_tests_deploy-src.jar",
    ],
    deps = [
        ":org_ray_ray_api",
        ":org_ray_ray_runtime",
        ":org_ray_ray_tutorial",
        ":org_ray_ray_streaming",
        ":all_tests",
        ":streaming_tests",
    ],
)

define_java_module(
    name = "api",
    deps = [
        "@org_slf4j_slf4j_log4j12//jar",
        "@log4j_log4j//jar",
        "@javax_xml_bind_jaxb_api//jar",
        "@com_sun_xml_bind_jaxb_core//jar",
        "@com_sun_xml_bind_jaxb_impl//jar",
    ],
)

define_java_module(
    name = "runtime",
    additional_srcs = [
        ":generate_java_gcs_fbs",
    ],
    additional_resources = [
        ":java_native_deps",
    ],
    define_test_lib = True,
    test_deps = [
        ":org_ray_ray_api",
        ":org_ray_ray_runtime",
        "@org_apache_commons_commons_lang3//jar",
        "@com_beust_jcommander//jar",
        "@org_testng_testng//jar",
        "@commons_io_commons_io//jar",
    ],
    deps = [
        ":org_ray_ray_api",
        "@plasma//:org_apache_arrow_arrow_plasma",
        "@org_slf4j_slf4j_api//jar",
        "@com_fasterxml_jackson_core_jackson_core//jar",
        "@org_apache_commons_commons_pool2//jar",
        "@org_javassist_javassist//jar",
        "@org_objenesis_objenesis//jar",
        "@com_typesafe_config//jar",
        "@org_apache_commons_commons_lang3//jar",
        "@de_ruedigermoeller_fst//jar",
        "@com_github_davidmoten_flatbuffers_java//jar",
        "@redis_clients_jedis//jar",
        "@commons_io_commons_io//jar",
        "@com_google_guava_guava//jar",
        "@org_slf4j_slf4j_log4j12//jar",
        "@org_ow2_asm_asm//jar",
    ],
)

define_java_module(
    name = "tutorial",
    deps = [
        ":org_ray_ray_api",
        ":org_ray_ray_runtime",
        "@com_google_guava_guava//jar",
    ],
)

define_java_module(
    name = "test",
    deps = [
        ":org_ray_ray_api",
        ":org_ray_ray_runtime",
        "@org_apache_commons_commons_lang3//jar",
        "@org_slf4j_slf4j_api//jar",
        "@plasma//:org_apache_arrow_arrow_plasma",
        "@org_testng_testng//jar",
        "@com_google_guava_guava//jar",
        "@commons_io_commons_io//jar",
    ],
)

define_java_module(
    name = "streaming",
    deps = [
        ":org_ray_ray_api",
        ":org_ray_ray_runtime",
        "@com_google_guava_guava//jar",
        "@org_slf4j_slf4j_api//jar",
        "@org_slf4j_slf4j_log4j12//jar",
    ],
    define_test_lib = True,
    test_deps = [
        ":org_ray_ray_api",
        ":org_ray_ray_runtime",
        ":org_ray_ray_streaming",
        "@com_beust_jcommander//jar",
        "@com_google_guava_guava//jar",
        "@org_slf4j_slf4j_api//jar",
        "@org_slf4j_slf4j_log4j12//jar",
        "@org_testng_testng//jar",
    ],
)

java_binary(
    name = "all_tests",
    main_class = "org.testng.TestNG",
    data = ["testng.xml"],
    args = ["java/testng.xml"],
    runtime_deps = [
        ":org_ray_ray_test",
        ":org_ray_ray_runtime_test",
    ],
)

java_binary(
    name = "streaming_tests",
    main_class = "org.testng.TestNG",
    data = ["streaming/testng.xml"],
    args = ["java/streaming/testng.xml"],
    runtime_deps = [
        ":org_ray_ray_streaming_test",
    ],
)

flatbuffers_generated_files = [
    "ActorCheckpointData.java",
    "ActorCheckpointIdData.java",
    "ActorState.java",
    "ActorTableData.java",
    "Arg.java",
    "ClassTableData.java",
    "ClientTableData.java",
    "ConfigTableData.java",
    "CustomSerializerData.java",
    "DriverTableData.java",
    "ErrorTableData.java",
    "ErrorType.java",
    "FunctionTableData.java",
    "GcsTableEntry.java",
    "HeartbeatBatchTableData.java",
    "HeartbeatTableData.java",
    "Language.java",
    "ObjectTableData.java",
    "ProfileEvent.java",
    "ProfileTableData.java",
    "RayResource.java",
    "ResourcePair.java",
    "SchedulingState.java",
    "TablePrefix.java",
    "TablePubsub.java",
    "TaskInfo.java",
    "TaskLeaseData.java",
    "TaskReconstructionData.java",
    "TaskTableData.java",
    "TaskTableTestAndUpdate.java",
]

flatbuffer_java_library(
    name = "java_gcs_fbs",
    srcs = ["//:gcs_fbs_file"],
    outs = flatbuffers_generated_files,
    out_prefix = "",
)

genrule(
    name = "generate_java_gcs_fbs",
    srcs = [":java_gcs_fbs"],
    outs = [
        "runtime/src/main/java/org/ray/runtime/generated/" + file for file in flatbuffers_generated_files
    ],
    cmd = """
        for f in $(locations //java:java_gcs_fbs); do
            chmod +w $$f
            cp -f $$f $(@D)/runtime/src/main/java/org/ray/runtime/generated
        done
        python $$(pwd)/java/modify_generated_java_flatbuffers_files.py $(@D)/..
    """,
    local = 1,
)

filegroup(
    name = "java_native_deps",
    srcs = [
        "//:redis-server",
        "//:libray_redis_module.so",
        "//:raylet",
        "//:raylet_library_java",
        "@plasma//:plasma_store_server",
        "@plasma//:plasma_client_java",
    ],
)

# Generates the depedencies needed by maven.
genrule(
    name = "gen_maven_deps",
    srcs = [
        ":java_native_deps",
        ":generate_java_gcs_fbs",
        "@plasma//:org_apache_arrow_arrow_plasma",
    ],
    outs = ["gen_maven_deps.out"],
    cmd = """
        set -x
        WORK_DIR=$$(pwd)
        # Copy native dependecies.
        NATIVE_DEPS_DIR=$$WORK_DIR/java/runtime/native_dependencies/
        rm -rf $$NATIVE_DEPS_DIR
        mkdir -p $$NATIVE_DEPS_DIR
        for f in $(locations //java:java_native_deps); do
            chmod +w $$f
            cp $$f $$NATIVE_DEPS_DIR
        done
        # Copy flatbuffers-generated files
        GENERATED_DIR=$$WORK_DIR/java/runtime/src/main/java/org/ray/runtime/generated
        rm -rf $$GENERATED_DIR
        mkdir -p $$GENERATED_DIR
        for f in $(locations //java:generate_java_gcs_fbs); do
            cp $$f $$GENERATED_DIR
        done
        # Install plasma jar to local maven repo.
        mvn install:install-file -Dfile=$(locations @plasma//:org_apache_arrow_arrow_plasma) -Dpackaging=jar \
            -DgroupId=org.apache.arrow -DartifactId=arrow-plasma -Dversion=0.13.0-SNAPSHOT
        echo $$(date) > $@
    """,
    local = 1,
)
